package com.jackrain.clouddata.oms.api;

import com.alibaba.fastjson.JSONObject;
import com.jackrain.clouddata.oms.dao.UploadRetailDao;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.query.QueryEngine;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * by bigzhi
 */

@RestController
public class UploadRetail {

    @RequestMapping(path = "/api/oms/uploadRetail",method = RequestMethod.POST)
    public JSONObject uploadRetail(HttpServletRequest request){
        UploadRetailDao uploadRetailDao = ApplicationContextHandle.getBean(UploadRetailDao.class);
        String jsonString = request.getParameter("DATA");
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        String tag = request.getHeader("TAG");
        String queryTopicSql = "SELECT TOPIC FROM OMS_TOPIC WHERE TOPIC='OMS_M_RETAIL_BACK'";
        JSONObject topicJson = QueryEngine.getInstance().doQueryObject(queryTopicSql);
        String topic = topicJson.getString("TOPIC");

        if(jsonObject != null && jsonObject.size() > 0) {
            uploadRetailDao.insertRetail(jsonObject, tag, topic);
        }

        ValueHolder valueHolder = new ValueHolder();
        valueHolder.put("code",0);
        return valueHolder.toJSONObject();
    }
}
